package com.bls.dao.impl;

import com.bls.dao.AnalyseDao;
import com.bls.entity.Analyse;
import com.fy.toolhelper.db.BaseDaoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author xiaoliang
 * @date 2022/01/10 20:11
 **/
public class AnalyseDaoImpl extends BaseDaoImpl<Analyse> implements AnalyseDao {
    public AnalyseDaoImpl() throws Exception {
    }

    @Override
    public Analyse selectAnalyseById(Connection connection, Long analyseId) throws SQLException {
        String sql = "SELECT * FROM bls_analyse WHERE id = ?";
        PreparedStatement pstm = connection.prepareStatement(sql);
        pstm.setLong(1, analyseId);
        ResultSet rs = pstm.executeQuery();
        Analyse analyse = null;
        while (rs.next()) {
            analyse = getAnalyseEntity(rs);
        }
        if (rs != null) {
            rs.close();
        }
        if (pstm != null) {
            pstm.close();
        }
        return analyse;
    }

    @Override
    public int deleteById(Connection connection, long anylyseId) throws SQLException {

        String sql = "DELETE FROM bls_analyse WHERE id = ?";
        PreparedStatement pstm = connection.prepareStatement(sql);
        pstm.setLong(1, anylyseId);
        int i = pstm.executeUpdate();
        if (pstm != null) {
            pstm.close();
        }
        return i;
    }


    private Analyse getAnalyseEntity(ResultSet rs) throws SQLException {
        Analyse analyse = new Analyse();
        analyse.setId(rs.getLong("id"));
        analyse.setCode(rs.getString("code"));
        analyse.setStatus(rs.getInt("status"));
        analyse.setDeveloperId(rs.getLong("developer_id"));
        analyse.setBandId(rs.getLong("band_id"));
        analyse.setCreateTime(rs.getTimestamp("create_time"));
        analyse.setUpdateTime(rs.getTimestamp("update_time"));
        return analyse;
    }
}
